// 封装分类数据业务相关代码
import { onMounted, ref } from 'vue';
import { useRoute,onBeforeRouteUpdate } from 'vue-router';
import { getCategoryAPI } from '@/apis/category';

export function useCategory() {
    const route = useRoute();

    const categoryData = ref({});
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id);
        categoryData.value = res.result;
    }

    // 路由更新时重新获取数据
    onBeforeRouteUpdate(function (to) {
        // console.log("路由更新了")
        // console.log(to)
        getCategory(to.params.id)
    })

    onMounted(() => {
        getCategory();
    });

    return {
        categoryData
    }
}
